package top.codeora.aiollama.app;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import top.codeora.aiollama.advisor.MySimpleLoggerAdvisor;

@Component
@Slf4j
public class CodeOraApp {

    @Resource
    private ToolCallback[] tools;

    private final ChatClient chatClient;

    private static final String SYSTEM_PROMPT = "现在你扮演问答专家，开场向用户表明身份，你的名字叫OllamaAI助手。告知用户可以询问问题";

    public CodeOraApp(ChatModel ollamaChatModel) {
        chatClient = ChatClient.builder(ollamaChatModel)
                .defaultSystem(SYSTEM_PROMPT)
                .build();
    }

    public String doChat(String message, String chatId) {
        ChatResponse chatResponse = chatClient.prompt()
                .user(message)
                .call()
                .chatResponse();
        String text = chatResponse.getResult().getOutput().getText();
        log.info("用户：{}", message);
        log.info("ollama-ai：{}", text);
        return text;
    }

    public String doChat2Advisor(String message, String chatId) {
        ChatResponse chatResponse = chatClient.prompt()
                .user(message)
                .advisors(new MySimpleLoggerAdvisor())
                .call()
                .chatResponse();
        String text = chatResponse.getResult().getOutput().getText();
        log.info("用户：{}", message);
        log.info("ollama-ai：{}", text);
        return text;
    }


    public String doChat2Tool(String message, String chatId) {
        ChatResponse chatResponse = chatClient.prompt()
                .user(message)
//                .advisors(new ReReadingAdvisor())
                .advisors(new MySimpleLoggerAdvisor())
                .tools(tools)
                .call()
                .chatResponse();
        String text = chatResponse.getResult().getOutput().getText();
        log.info("用户：{}", message);
        log.info("ollama-ai：{}", text);
        return text;
    }

    public  Flux<ChatResponse> doChat2Stream(String message, String chatId) {
        Flux<ChatResponse> responseFlux = chatClient.prompt()
                .user(message)
                .advisors(new MySimpleLoggerAdvisor())
                .tools(tools)
                .stream()
                .chatResponse();
        return responseFlux;
    }


}
